//
//  ViewController.m
//  TestLocation
//
//  Created by changlinwang on 14-9-10.
//  Copyright (c) 2014年 gem. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //判断设备的定位服务是否可以使用
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManage=[[CLLocationManager alloc] init];
        self.locationManage.delegate=self;
    }else{
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"定位不可以用" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
    
    
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)locationManager:(CLLocationManager *)manager
	 didUpdateLocations:(NSArray *)locations{
     NSLog(@"%@",locations);
    CLLocation *location=[locations firstObject];
    self.latigude.text=[NSString stringWithFormat:@"%0.4f",location.coordinate.latitude];
    self.longitude.text=[NSString stringWithFormat:@"%0.4f",location.coordinate.longitude];
    [self.locationManage stopUpdatingLocation];
    //怎样根据经纬度 知道你在哪里
    //有API  根据经纬度知道所在的省份和位置信息
    //百度地图有这个API
}

- (void)locationManager:(CLLocationManager *)manager
    didFailWithError:(NSError *)error{
    NSLog(@"%@",error);
    NSString *errorMsg=[NSString stringWithFormat:@"%@",error];
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:errorMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
    

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)toggleSwitch:(id)sender {
//    self.toggle.on=YES;
//    NSLog(@"%d",self.toggle.isOn);
    if (self.toggle.isOn) {
        [self.locationManage startUpdatingLocation];
    }else{
        [self.locationManage stopUpdatingLocation];
    }
}
@end
